package com.sise.thread.反射;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 作者：idea
 * 日期：2018/6/27
 * 描述：
 */
public class Test {
    public static void main(String[] args) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        User user=new User();
        Class userClass=user.getClass();
        if(userClass.isAnnotationPresent(Table.class)) {
            Annotation[] annotations = userClass.getAnnotations();
            for (Annotation annotation : annotations) {
                Table table= (Table) annotation;
                System.out.println(table.name());
            }
        }


        Field[] fields=user.getClass().getDeclaredFields();
        for (Field field : fields) {
            if(field.getName().equals("id")){
                field.setAccessible(true);
                System.out.println("this is id");
                    PropertyDescriptor pd=new PropertyDescriptor(field.getName(),user.getClass());
                    Method method=pd.getWriteMethod();
                    method.invoke(user,10);
                System.out.println(user.getId());
            }
        }

    }
}
